package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.dbmodel.SysUser;
import cn.com.yx.ms.domain.sys.UserChangePwdModel;
import cn.com.yx.ms.domain.sys.UserModel;
import cn.com.yx.ms.domain.sys.UserResetPwdModel;
import cn.com.yx.ms.service.ISysService;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "用户", description = "用户资源获取")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private MessageSource messageSource;

    @Resource
    private ISysService iSysService;


    @PostMapping("user")
    @ApiOperation(value = "创建用户信息", notes = "创建用户信息")
    @ApiImplicitParam(paramType = "body", name = "userModel", dataType = "Map", required = true, value = "用户实体详细信息")
    public ApiResult create(@RequestBody @Validated UserModel userModel, BindingResult bindingResult) throws Exception {
        ApiCode apiCode = iSysService.saveUser(userModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_120500) || apiCode.equals(ApiCode.APP_120501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @DeleteMapping("users/{id}")
    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @ApiImplicitParam(paramType = "path", name = "id", dataType = "String", required = true, value = "用户id")
    public ApiResult delete(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.deleteUser(id));
    }

    @PutMapping("user")
    @ApiOperation(value = "更新部门信息", notes = "更新用户信息")
    @ApiImplicitParam(paramType = "body", name = "orgModel", dataType = "Map", required = true, value = "用户实体详细信息")
    public ApiResult updateModel(@RequestBody @Validated UserModel userModel, BindingResult bindingResult) throws Exception {
        ApiCode apiCode = iSysService.saveUser(userModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_120500) || apiCode.equals(ApiCode.APP_120501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @PatchMapping("user")
    @ApiOperation(value = "用户信息部分更新", notes = "用户信息部分更新")
    @ApiImplicitParam(paramType = "body", name = "userModel", dataType = "Map", required = true, value = "用户信息")
    public ApiResult updateProperty(@RequestBody @Validated UserModel userModel, BindingResult bindingResult) throws Exception {
        ApiCode apiCode = iSysService.saveUser(userModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_120500) || apiCode.equals(ApiCode.APP_120501),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @ResponseBody
    @RequestMapping(value = "users", method = RequestMethod.GET)
    @ApiOperation("获取所有部门信息")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        //System.out.println(BaseConstant.getAesConfig().getSecretKey());


        query.getConditions().put("title", request.getParameter("title"));
        query.getConditions().put("count", request.getParameter("count"));
        query.getConditions().put("starttime", request.getParameter("starttime"));
        query.getConditions().put("endtime", request.getParameter("endtime"));

        //return new ApiResult<>(AppConstant.MSG_SUCCESS, iTestService.find(query));
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "users/page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的用户信息")
    public ApiResult findPage(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("orgid", request.getParameter("orgid"));
        query.getConditions().put("ename", request.getParameter("ename"));
        query.getConditions().put("cname", request.getParameter("cname"));

        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<SysUser> page = (Page<SysUser>) iSysService.findUser(query);
        ApiPager<SysUser> apiPager = new ApiPager<SysUser>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "users/{id}", method = GET)
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "用户ID")
    @ApiOperation("获取单个用户信息")
    public ApiResult findById(@PathVariable("id") String id) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iSysService.findOneUser(id));

    }

    @PatchMapping("users/password")
    @ApiOperation(value = "密码修改", notes = "密码修改")
    @ApiImplicitParam(paramType = "body", name = "userChangePwdModel", dataType = "Map", required = true, value = "用户信息")
    public ApiResult updatePwd(@RequestBody @Validated UserChangePwdModel userChangePwdModel) throws Exception {
        ApiCode apiCode = iSysService.updatePwd(userChangePwdModel);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_100200),
                MsgPropertyUtil.getProperty(apiCode.msgId));

    }
    @GetMapping("apply/resetPwd/{account}")
    @ApiOperation(value = "添加重置密码记录",notes="添加重置密码记录")
    @ApiImplicitParam(paramType = "String", name = "id", dataType = "String", required = true, value = "账户信息")
    public ApiResult applyResetPwd(@PathVariable("account")String account){
        ApiCode apiCode=iSysService.applyResetPwd(account);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_120502),
                MsgPropertyUtil.getProperty(apiCode.msgId));
    }
    @GetMapping("submit/resetPwd/{userid}")
    @ApiOperation(value="密码重置请求",notes = "密码重置")
    @ApiImplicitParam(paramType = "String" ,name = "id",dataType = "String", required=true,value="用户id")
    public ApiResult subResetPwd(@PathVariable("userid")String userid) throws Exception{
        ApiCode apiCode=iSysService.submitResetPwd(userid);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),apiCode.equals(ApiCode.APP_120503),MsgPropertyUtil.getProperty(apiCode.msgId));
    }

    @ResponseBody
    @RequestMapping(value = "resetPwd/page", method = RequestMethod.GET)
    @ApiOperation("密码重置申请记录")
    public ApiResult FindResetPwd(@ApiQuery ApiQueryBase query,HttpServletRequest request){
        // 查询条件接受
        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("cname",request.getParameter("cname"));
        query.getConditions().put("ename",request.getParameter("ename"));
        query.getConditions().put("userid",request.getParameter("userid"));
        // 翻页对象构建
        PageHelper.startPage(query.getPi(), query.getPs(), true);
        Page<UserResetPwdModel> page = (Page<UserResetPwdModel>) iSysService.findResertPwd(query,request);
        ApiPager<UserResetPwdModel> apiPager = new ApiPager<UserResetPwdModel>(query.getPs(),
                query.getPi(),
                page.getTotal(),
                page.getResult());

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", apiPager);

    }

    @ResponseBody
    @RequestMapping(value = "users/{userid}/btnfuns", method = GET)
    @ApiImplicitParam(paramType = "String", name = "userid", dataType = "String", required = true, value = "用户ID")
    @ApiOperation("获取单个用户信息下的功能菜单集合")
    public ApiResult findFunsByRoleId(@PathVariable("userid") String userid) {
        return new ApiResult().success(AppConstant.MSG_SUCCESS, "", iSysService.findOnlyBtnFunsByUserId(userid));
    }
}